<?php
#################
# This is the main Totaliser display.
# And reads the horse DB to display the 
# infomation for the current race betting 
# The Page is set to refresh every 15 seconds;
# If your server can cope this can be set as low as 2 or 3 seconds
# If you are running this on a big screen set font Size Higher if just for mobile/laptops set to 5 or 4
$col=array('<FONT color="green" size=6>','<FONT color="yellow" size=6>','<FONT color="blue" size=6>','<FONT color="white" size=6>','<FONT color="orange" size=6>','<FONT color="purple" size=6>','<FONT color="lightblue" size=6>','<FONT color="red" size=6>') ;
$col1 = '<FONT color="green">';
$n=0;
echo <<<endHTML
<html>
<head><title>Race night Tote page</title></head>
<body bgcolor = "black">
<center> <FONT color="#005CB9"  SIZE=6> <b>
Welcome to your race night <br>
Live race totaliser  </b>  </font>
endHTML;
 $db=sqlite_open("horse.db");
 $chresult=sqlite_query($db,"SELECT horse_name from race Where horse_num = 9");
 $cm = sqlite_fetch_single($chresult) ;
echo '<center> <FONT color="#005CB9"  SIZE=5> <b> Current race is the <br><FONT color="YELLOW">' .$cm ;
 sqlite_close($db); 
echo <<<endHTML
<FONT  SIZE=3 > <b>
<table border="1">
<tr> 
<th> <FONT color="#005CB9" size=4>Horse num </th>  <th> <FONT color="#005CB9" size=4>Horse name </th><th> <FONT color="#005CB9" size=4>Number of bets </th><th> <FONT color="#005CB9" size=4>Payout </th>
</tr>    
endHTML;
$db=sqlite_open("horse.db");
$result = sqlite_array_query($db, 'SELECT * FROM race  LIMIT 8', SQLITE_ASSOC);
#$res = sqlite_fetch_single($result)  ; 

foreach ($result as $entry) { 
 #echo'<tr>';     

#echo ;
  echo  '<td>'.$col[$n] . $entry['horse_num'] .'</td><td>' .$col[$n]. $entry['horse_name'].'</td><td>'.$col[$n]. $entry['bets'] .' </td><td> '.$col[$n]. $entry['payout'].'</td>';
echo $col[0];
echo '</tr>'; 
$n++;
}
echo '</table> '  ;
echo'<center> <FONT color="#005CB9"  SIZE=5>';
#echo $col1;
$cresult=sqlite_query($db,"SELECT payout from race Where horse_num = 9");
 $res = sqlite_fetch_single($cresult)  ;
 $TCF = round($res/10);
 $TCFP = $TCF * 10 ;
 echo 'total cash for payouts :'.$TCFP ;
 $chresult=sqlite_query($db,"SELECT decimal_odds from race Where horse_num = 9");
$cm = sqlite_fetch_single($chresult) ;
echo '<br>'.'charity money raised though betting'.':-'.$cm   ;
#Page refresh rate the content section number sets the refresh rate
echo <<<endHTML
<p> Race winners and payouts <br>

  <meta http-equiv="refresh" content="15;url=index.php">

 
endHTML;
#exit;
$r="race";
$nr=1;
$cres=true;
$finished = null;
While($nr < 9) {
#while ($cres==true){
$rn=$r.$nr;
$cresult=sqlite_query($db,"SELECT payout from $rn Where decimal_odds = 1");
$aresult=sqlite_query($db,"SELECT horse_name from $rn Where decimal_odds = 1");
$cres = sqlite_fetch_single($cresult)  ;
$ares = sqlite_fetch_single($aresult)  ;
if ($cres==false)  
echo "" ;
Else
Echo 'Winner of  '. $rn.' was '. $ares. ' payout was '. $cres.'<br>' ;
$nr++ ;
 }
#echo "hold"; 
#endif;
#endif ;
#endwhile;

sqlite_close($db);
echo $finished
?>